from django.shortcuts import render
from django.views import View
# Create your views here.
from django.http import HttpResponse, JsonResponse
from random import randint
from django_redis import get_redis_connection
# 引入图片验证码的包
from meiduo_mall.libs.captcha.captcha import Captcha
# 手机号验证码   云通讯的包
from meiduo_mall.libs.yuntongxun.sms import CCP
# 引入模型类里面的User类
from users.models import User
# 异步发送短信
from celery_tasks.code_sms.tasks import send_sms_code
from threading import Thread


#  ------------------------------------------------判断图片验证码-------------------------------------------------

class ImgaeView(View):
    def get(self, request, uuid):
        print('123--------------------------------------------------')
        # 1.生成图片验证
        captcha = Captcha.instance()
        # data 自动生成的uuid   text  验证码的信息  image 图片验证码
        data, text, image = captcha.generate_captcha()
        # 2.将图片验证码存储到服务器redis上
        client = get_redis_connection('verify_code')
        client.setex('image_code_%s' % uuid, 60 * 5, text)
        # 3.返回验证码图片
        return HttpResponse(image, content_type='image/jpg')
#  ---------------------------------------------手机号-------------------------------------------------------
class SmsCodeView(View):
    # mobile  前端传入的手机号
    def get(self, request, mobile):
        client = get_redis_connection('verify_code')
        # 判断两次请求的时间间隔是否在 60秒内
        flag_data = client.get('sms_flag_%s' % mobile)
        if flag_data:
            return HttpResponse('请求过于频繁', status=400)
        # 1.获取前端数据
            # 获取图片验证码的数据      获取前端验证码的uuid
        image_code = request.GET.get('image_code')
        uuid = request.GET.get('image_code_id')
        # 2.生成短信验证码
        sms_code = '%06d' % randint(0, 999999)
        print(sms_code)
        # 3.验证图片验证码
            # 连接redis数据库    获取uuid的数据
        client = get_redis_connection('verify_code')
        real_code = client.get('image_code_%s' % uuid)
        # 没有找得到uuid的信息
        if real_code is None:
            return HttpResponse('图片验证失败', status=400)
        if image_code.lower() != real_code.decode().lower():
            return HttpResponse('输入的验证码有误')
        # 4.使用celery的异步任务完成发送短信
        #  异步请求-------------------------------------------------
        send_sms_code.delay(mobile, sms_code)
        # 5. 保存生成的短信验证码到redis中
        # 开启redis管道
        pipline = client.pipeline()
        # 将Redis请求添加到队列
        pipline.setex('sms_code_%s' % mobile, 60 * 5, sms_code)
        pipline.setex('sms_flag_%s' % mobile, 60, 123)

        # 使用管道发送数据
        pipline.execute()
        # 6.返回结果
        return JsonResponse({'code': '0'})


class UsernameCount(View):
    '''
    判断用户名是否重复
    '''

    def get(self, request, username):

        # 1.获取前端传入的用户名
        # 2.查询用户名对应对象数量
        count = User.objects.filter(username=username).count()
        return JsonResponse({'count':count})

class MobileCount(View):
    '''
    判断手机号是否重复
    '''
    def get(self,request,mobile):
        # 1.获取前端传递的用户名
        # 2.查询用户名所对应对象数量
        count= User.objects.filter(mobile=mobile).count()
        # 3.返回查询到的数量
        return JsonResponse({'count':count})


















